package com.sugar.follow.up.module.business.dal.mysql.followuprecord;


import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.follow.up.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.follow.up.module.business.dal.dataobject.followuprecord.FollowUpRecordDO;
import com.sugar.follow.up.module.business.dal.dataobject.followuptemplate.FollowUpTemplateDO;
import com.sugar.follow.up.module.business.dal.dataobject.userpatient.UserPatientDO;
import com.sugar.follow.up.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;
import com.sugar.follow.up.module.business.controller.admin.followuprecord.vo.*;

/**
 * 随访发起记录 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface FollowUpRecordMapper extends BaseMapperX<FollowUpRecordDO> {

    default PageResult<FollowUpRecordRespVO> selectPage(FollowUpRecordPageReqVO reqVO, Long doctorId) {
        Long[] userIds = reqVO.getUserId();
        Long userId = null;
        Long patientId = null;
        if (userIds != null && userIds.length == 2) {
            userId = userIds[0];
            patientId = userIds[1];
        }
        return selectJoinPage(reqVO, FollowUpRecordRespVO.class, new MPJLambdaWrapperX<FollowUpRecordDO>()
                .selectAll(FollowUpRecordDO.class)
                .selectAs(AdminUserDO::getNickname, FollowUpRecordRespVO::getAdminUserName)
                .selectAs(UserPatientDO::getName, FollowUpRecordRespVO::getPatientName)
                .selectAs(UserPatientDO::getMobile, FollowUpRecordRespVO::getPatientMobile)
                .selectAs(FollowUpTemplateDO::getTitle, FollowUpRecordRespVO::getTemplateName)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, FollowUpRecordDO::getAdminUserId)
                .leftJoin(UserPatientDO.class, UserPatientDO::getId, FollowUpRecordDO::getPatientId)
                .leftJoin(FollowUpTemplateDO.class, FollowUpTemplateDO::getId, FollowUpRecordDO::getTemplateId)
                .eqIfPresent(FollowUpRecordDO::getTemplateId, reqVO.getTemplateId())
                .eqIfPresent(FollowUpRecordDO::getUserId, userId)
                .eqIfPresent(FollowUpRecordDO::getPatientId, patientId)
                .eqIfPresent(FollowUpRecordDO::getAdminUserId, doctorId)
                .likeIfPresent(AdminUserDO::getNickname, reqVO.getAdminUserId())
                .likeIfPresent(FollowUpRecordDO::getTitle, reqVO.getTitle())
                .betweenIfPresent(FollowUpRecordDO::getBeginTime, reqVO.getBeginTime())
                .betweenIfPresent(FollowUpRecordDO::getEndTime, reqVO.getEndTime())
                .betweenIfPresent(FollowUpRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(FollowUpRecordDO::getId));
    }

}